package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.bean.CustomerBean;
import com.shd.edanpei.bean.ImgInfoBean;
import com.shd.edanpei.bean.PicBean;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.enums.OrderTypeEnum;
import com.shd.edanpei.enums.PicEnum;
import com.shd.edanpei.presenter.FileListPresenter;
import com.shd.edanpei.presenter.contract.FileListContract;
import com.shd.edanpei.util.GsonUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/4/4.
 * 上传文件列表-》 2019年新添加  个人信用贷款和企业税金贷款
 */

public class FileListPersonalAndEnterpActivity extends BaseActivity<FileListPresenter>
        implements FileListContract.FileListIView, View.OnClickListener {


    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.tv_hint)
    TextView tvHint;
    @BindView(R.id.rl_identity)
    View identity;
    @BindView(R.id.tv_identity_status)
    TextView tvIdentityStatus;
    @BindView(R.id.rl_householdRegister)
    View householdRegister;
    @BindView(R.id.tv_householdRegister_status)
    TextView tvHouseholdRegisterStatus;
    //    @BindView(R.id.rl_bankPic)
//    View bankPic;
//    @BindView(R.id.tv_bankPic_status)
//    TextView tvBankPicStatus;
    @BindView(R.id.rl_roomCert)
    View roomCert;
    @BindView(R.id.house_text)
    View house_text;
    @BindView(R.id.tv_roomCert_status)
    TextView tvRoomCertStatus;
    @BindView(R.id.rl_businessCert)
    View businessCert;
    @BindView(R.id.tv_businessCert_status)
    TextView tvBusinessCertStatus;
    //    @BindView(R.id.rl_companyStream)
//    View companystream;
//    @BindView(R.id.tv_companyStream_status)
//    TextView tvCompanyStream;
    @BindView(R.id.rl_otherHouseCert)
    View otherHouseCert;
    @BindView(R.id.tv_otherHouseCert_status)
    TextView tvOtherHouseCertStatus;
    @BindView(R.id.rl_carCert)
    View carCert;
    @BindView(R.id.tv_carCert_status)
    TextView tvCarCertStatus;
    @BindView(R.id.rl_carWarranty)
    View carWarranty;
    @BindView(R.id.tv_carWarranty_status)
    TextView tvCarWarrantyStatus;
    @BindView(R.id.rl_maritalCert)
    View maritalCert;
    @BindView(R.id.tv_maritalCert_status)
    TextView tvMaritalCertStatus;
    @BindView(R.id.rl_zhengxin)
    View creditCert;
    @BindView(R.id.tv_zhengxin_status)
    TextView tvCreditCertStatus;
    @BindView(R.id.rl_supplement)
    View supplement;
    @BindView(R.id.tv_supplement_status)
    TextView tvSupplementStatus;

    @BindView(R.id.tv_tag2)
    View tag2;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;


    private Bundle bundle = new Bundle();
    private PicBean picBean = new PicBean();
    //订单类型
    private int orderType;
    private int orderID;
    private CustomerBean customerBean;
    private int type;

    private ImgInfoBean imgInfoBean;
    private int from;

    private boolean house;
    private boolean marry;
    private boolean fun;


    @Override
    protected int setLayout() {
        return R.layout.activity_file_list_personal_enterp;
    }

    @Override
    protected FileListPresenter bindPresenter() {
        return new FileListPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);
        headline.setText("上传文件");
        Bundle extras = getIntent().getExtras();
        from = extras.getInt(Constant.FROM);
        if (from == 1) {
//            orderType = extras.getInt(Constant.ORDER_TYPE);
            customerBean = extras.getParcelable(Constant.DATA);
//            picBean.setOrderId(customerBean.getOrderId());
//           changUI();
            orderType = extras.getInt(Constant.ORDER_TYPE);
            orderID = extras.getInt(Constant.ORDER_ID);
            mPresenter.getFileData(orderID,extras);

            headline.setText("修改上传文件");
            identity.setVisibility(View.GONE);
            tvHint.setVisibility(View.VISIBLE);
        } else {
            type = extras.getInt(Constant.TYPE);
            if (type == Constant.EDIT) {
                orderType = extras.getInt(Constant.ORDER_TYPE);
                customerBean = extras.getParcelable(Constant.DATA);
                picBean = extras.getParcelable("images");
                picBean.setOrderId(customerBean.getOrderId());
                changUI();
                checkUpStatus(picBean);
                tvHint.setVisibility(View.VISIBLE);
            } else if (type == Constant.SHOW) {
                imgInfoBean = extras.getParcelable(Constant.DATA);
                showUI();
            }
        }
    }

    private void showUI() {
        if (from != 1) {
            if (imgInfoBean.getIdCardPic() != null && imgInfoBean.getIdCardPic().size() > 0) {
                identity.setVisibility(View.VISIBLE);
                tvIdentityStatus.setText("查看");
                picBean.setIdCardPic(GsonUtil.toJson(imgInfoBean.getIdCardPic()));
            } else {
                identity.setVisibility(View.GONE);
            }
        } else {
            identity.setVisibility(View.GONE);
        }
        if (imgInfoBean.getHouseholdRegister() != null && imgInfoBean.getHouseholdRegister().size() > 0) {
            householdRegister.setVisibility(View.VISIBLE);
            tvHouseholdRegisterStatus.setText("查看");
            picBean.setHouseholdRegister(GsonUtil.toJson(imgInfoBean.getHouseholdRegister()));
        } else {
            householdRegister.setVisibility(View.GONE);
        }

        if (imgInfoBean.getRoomCert() != null && imgInfoBean.getRoomCert().size() > 0) {
            roomCert.setVisibility(View.VISIBLE);
            tvRoomCertStatus.setText("查看");
            picBean.setRoomCert(GsonUtil.toJson(imgInfoBean.getRoomCert()));
        } else {
            roomCert.setVisibility(View.GONE);
        }
        if (imgInfoBean.getBusinessCert() != null && imgInfoBean.getBusinessCert().size() > 0) {
            businessCert.setVisibility(View.VISIBLE);
            tvBusinessCertStatus.setText("查看");
            picBean.setBusinessCert(GsonUtil.toJson(imgInfoBean.getBusinessCert()));
        } else {
            businessCert.setVisibility(View.GONE);
        }

        if (imgInfoBean.getOtherHouseCert() != null && imgInfoBean.getOtherHouseCert().size() > 0) {
            otherHouseCert.setVisibility(View.VISIBLE);
            tvOtherHouseCertStatus.setText("查看");
            picBean.setOtherHouseCert(GsonUtil.toJson(imgInfoBean.getOtherHouseCert()));
        } else {
            otherHouseCert.setVisibility(View.GONE);
        }
        if (imgInfoBean.getCarCert() != null && imgInfoBean.getCarCert().size() > 0) {
            carCert.setVisibility(View.VISIBLE);
            tvCarCertStatus.setText("查看");
            picBean.setCarCert(GsonUtil.toJson(imgInfoBean.getCarCert()));
        } else {
            carCert.setVisibility(View.GONE);
        }
        if (imgInfoBean.getCarWarranty() != null && imgInfoBean.getCarWarranty().size() > 0) {
            carWarranty.setVisibility(View.VISIBLE);
            tvCarWarrantyStatus.setText("查看");
            picBean.setCarWarranty(GsonUtil.toJson(imgInfoBean.getCarWarranty()));
        } else {
            carWarranty.setVisibility(View.GONE);
        }
        if (imgInfoBean.getMaritalCert() != null && imgInfoBean.getMaritalCert().size() > 0) {
           // maritalCert.setVisibility(View.VISIBLE);
            tvMaritalCertStatus.setText("查看");
            picBean.setMaritalCert(GsonUtil.toJson(imgInfoBean.getMaritalCert()));
        } else {
            maritalCert.setVisibility(View.GONE);
        }
        if (imgInfoBean.getCreditCert() != null && imgInfoBean.getCreditCert().size() > 0) {
            creditCert.setVisibility(View.VISIBLE);
            tvCreditCertStatus.setText("查看");
            picBean.setCreditCert(GsonUtil.toJson(imgInfoBean.getCreditCert()));
        } else {
            creditCert.setVisibility(View.GONE);
        }
        if (imgInfoBean.getSupplement() != null && imgInfoBean.getSupplement().size() > 0) {
            supplement.setVisibility(View.VISIBLE);
            tvSupplementStatus.setText("查看");
            picBean.setSupplement(GsonUtil.toJson(imgInfoBean.getSupplement()));
        } else {
            supplement.setVisibility(View.GONE);
        }
        tag2.setVisibility(View.GONE);
        if (from == 1) {
            tvSubmit.setVisibility(View.VISIBLE);
            tvSubmit.setText("确认修改");
        } else {
            tvSubmit.setVisibility(View.GONE);
        }

        mPresenter.getOrderInfo(orderID);
    }
    @Subscribe(code = RxBusCode.NEED_DATA_SUBMIT_SUCCESS)
    public void doneNeedData(CustomerBean bean) {
       this.customerBean=bean;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (type == Constant.EDIT) {
            checkUpStatus(picBean);
        }
    }

    @Override
    protected void initListener() {
        identity.setOnClickListener(this);
        householdRegister.setOnClickListener(this);
//        bankPic.setOnClickListener(this);
        roomCert.setOnClickListener(this);
        businessCert.setOnClickListener(this);
//        companystream.setOnClickListener(this);
        otherHouseCert.setOnClickListener(this);
        carCert.setOnClickListener(this);
        carWarranty.setOnClickListener(this);
        maritalCert.setOnClickListener(this);
        creditCert.setOnClickListener(this);
        supplement.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        bundle.clear();
        switch (view.getId()) {
            case R.id.rl_identity:
                bundle.putInt(Constant.TYPE, PicEnum.idCardPic.getId());
                break;
            case R.id.rl_householdRegister:
                bundle.putInt(Constant.TYPE, PicEnum.householdRegister.getId());
                break;
            case R.id.rl_roomCert:
                bundle.putInt(Constant.TYPE, PicEnum.roomCert.getId());
                break;
            case R.id.rl_businessCert:
                bundle.putInt(Constant.TYPE, PicEnum.newBusinessCert.getId());
                break;
            case R.id.rl_otherHouseCert:
                bundle.putInt(Constant.TYPE, PicEnum.otherHouseCert.getId());
                break;
            case R.id.rl_carCert:
                bundle.putInt(Constant.TYPE, PicEnum.carCert.getId());
                break;
            case R.id.rl_carWarranty:
                bundle.putInt(Constant.TYPE, PicEnum.newWarranty.getId());
                break;
            case R.id.rl_maritalCert:
                bundle.putInt(Constant.TYPE, PicEnum.maritalCert.getId());
                break;
            case R.id.rl_zhengxin:
                bundle.putInt(Constant.TYPE, PicEnum.newCreditCert.getId());
                break;
            case R.id.rl_supplement:
                bundle.putInt(Constant.TYPE, PicEnum.supplement.getId());
                break;
            default:
                break;
        }
        if (from == 1) {
            bundle.putParcelable(Constant.DATA, picBean);
            startActivity(UpFileActivity.class, bundle);
        } else {
            if (type == Constant.EDIT) {
                bundle.putParcelable(Constant.DATA, picBean);
                startActivity(UpFileActivity.class, bundle);
            } else if (type == Constant.SHOW) {
                bundle.putParcelable(Constant.DATA, imgInfoBean);
                startActivity(PicShowActivity.class, bundle);
            }
        }

    }

    /**
     * 根据订单类型，显示隐藏需要上传文件
     */
    private void changUI() {
        house_text.setVisibility(View.GONE);
        if (orderType == OrderTypeEnum.car.getKey()) {
            carCert.setVisibility(View.VISIBLE);
            carWarranty.setVisibility(View.VISIBLE);
        } else if (orderType == OrderTypeEnum.house.getKey()) {
            roomCert.setVisibility(View.VISIBLE);
            if (customerBean.isHaveCompany()) {
                businessCert.setVisibility(View.VISIBLE);
//                companystream.setVisibility(View.VISIBLE);
            }
            if (customerBean.isHaveOtherHouse()) {
                otherHouseCert.setVisibility(View.VISIBLE);
            }

        }else if(orderType==OrderTypeEnum.personal.getKey()){
            creditCert.setVisibility(View.VISIBLE);
           if("1".equals(customerBean.getFund())||"1".equals(customerBean.getHouse())){
               if("1".equals(customerBean.getFund())&&"0".equals(customerBean.getHouse())){
                   house_text.setVisibility(View.VISIBLE);//选填文字提示
               }
               roomCert.setVisibility(View.VISIBLE);//公积金房产
           }else{
               roomCert.setVisibility(View.GONE);//公积金房产
           }
           // businessCert.setVisibility(View.VISIBLE);
            carWarranty.setVisibility("0".equals(customerBean.getInsurance())?View.GONE:View.VISIBLE);//保单

        }else if(orderType==OrderTypeEnum.enterprise.getKey()){
            creditCert.setVisibility(View.VISIBLE);
            businessCert.setVisibility(View.VISIBLE);
        }
        if (customerBean.isMarrage()) {
           // maritalCert.setVisibility(View.VISIBLE);
        }

    }

    /**
     * 检查是否已上传
     */
    private void checkUpStatus(PicBean picBean) {
//        if (!TextUtils.isEmpty(picBean.getIdCardPic())) {
//            tvIdentityStatus.setText("已上传");
//        }
        if (!TextUtils.isEmpty(picBean.getHouseholdRegister())) {
            tvHouseholdRegisterStatus.setText("已上传");
        }
        if (!TextUtils.isEmpty(picBean.getRoomCert())) {
            tvRoomCertStatus.setText("已上传");
        }
        if (!TextUtils.isEmpty(picBean.getBusinessCert())) {
            tvBusinessCertStatus.setText("已上传");
        }
//        if (!TextUtils.isEmpty(picBean.getCompanyStream())) {
//            tvCompanyStream.setText("已上传");
//        }
        if (!TextUtils.isEmpty(picBean.getOtherHouseCert())) {
            tvOtherHouseCertStatus.setText("已上传");
        }
        if (!TextUtils.isEmpty(picBean.getCarCert())) {
            tvCarCertStatus.setText("已上传");
        }
        if (!TextUtils.isEmpty(picBean.getCarWarranty())) {
            tvCarWarrantyStatus.setText("已上传");
        }
        if (!TextUtils.isEmpty(picBean.getMaritalCert())) {
            tvMaritalCertStatus.setText("已上传");
        }
        if (!TextUtils.isEmpty(picBean.getCreditCert())) {
            tvCreditCertStatus.setText("已上传");
        }
        if (!TextUtils.isEmpty(picBean.getSupplement())) {
            tvSupplementStatus.setText("已上传");
        }

        if (!TextUtils.isEmpty(picBean.getCarWarranty())) {//保单
            tvCarWarrantyStatus.setText("已上传");
        }
        if (!TextUtils.isEmpty(picBean.getBusinessCert())) {//营业执照
            tvBusinessCertStatus.setText("已上传");
        }
        if (!TextUtils.isEmpty(picBean.getCreditCert())) {//征信
            tvCreditCertStatus.setText("已上传");
        }
    }
//
//    /**
//     * 提示由请上传改为已上传
//     */
//    private void isUpDao(int id) {
//        TextView view = findViewById(id);
//        view.setText("已上传");
//
//        //TODO：此处不删
////        Drawable drawable = ContextCompat.getDrawable(this, R.drawable.ic_all_choose);
////        Drawable drawable1 = ContextCompat.getDrawable(this, R.drawable.ic_arrow);
////        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
////        drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
////        view.setCompoundDrawables(drawable, null, drawable1, null);
//    }


    @OnClick(R.id.iv_headline_back)
    void close() {
        RxBus.get().send(RxBusCode.SAVE_FILE_DATA, picBean);
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            close();
        }
        return false;
    }

    @OnClick(R.id.tv_submit)
    void submit() {
        if (from == 1) {
            if (orderType == OrderTypeEnum.car.getKey()) {
                mPresenter.updateCarFile(picBean, orderID);
            } else if (orderType == OrderTypeEnum.house.getKey()) {
                mPresenter.updateHouseFile(picBean, orderID);
            }else if(orderType==OrderTypeEnum.personal.getKey()){
                mPresenter.updateCreditAttachment(picBean, orderID);
            }else if(orderType ==OrderTypeEnum.enterprise.getKey()){
                mPresenter.updateTaxrAttachment(picBean, orderID);
            }
        } else {
            if (orderType == OrderTypeEnum.car.getKey()) {
                mPresenter.submitCarFile(picBean, customerBean);
            } else if (orderType == OrderTypeEnum.house.getKey()) {
               // mPresenter.submitHouseFile(picBean, customerBean);
                mPresenter.submitCreditAttachment(picBean, customerBean);
            }else if(orderType==OrderTypeEnum.personal.getKey()){
                mPresenter.submitCreditAttachment(picBean, customerBean);
            }else if(orderType ==OrderTypeEnum.enterprise.getKey()){
                mPresenter.submitTaxrAttachment(picBean, customerBean);
            }
        }
    }
//    @Subscribe(code = RxBusCode.house)
//    public void setHouse(Boolean b) {
//        house = b;
//    }
//
//    @Subscribe(code = RxBusCode.marry)
//    public void setMarry(Boolean b) {
//        marry = b;
//    }
//
//    @Subscribe(code = RxBusCode.fun)
//    public void setFun(Boolean b) {
//        fun = b;
//    }


    @Subscribe(code = RxBusCode.FILE_UPLOAD_SUCCESS)
    public void setFileData(PicBean bean) {
        int type = bean.getType();
//        if (type == PicEnum.idCardPic.getId()) {
//            picBean.setIdCardPic(bean.getIdCardPic());
//        } else
        if (type == PicEnum.householdRegister.getId()) {
            picBean.setHouseholdRegister(bean.getHouseholdRegister());
        } else if (type == PicEnum.roomCert.getId()) {
            picBean.setRoomCert(bean.getRoomCert());
        } else if (type == PicEnum.businessCert.getId()) {
            picBean.setBusinessCert(bean.getBusinessCert());
        } else if (type == PicEnum.otherHouseCert.getId()) {
            picBean.setOtherHouseCert(bean.getOtherHouseCert());
        } else if (type == PicEnum.carCert.getId()) {
            picBean.setCarCert(bean.getCarCert());
        } else if (type == PicEnum.carWarranty.getId()) {
            picBean.setCarWarranty(bean.getCarWarranty());
        } else if (type == PicEnum.maritalCert.getId()) {
            picBean.setMaritalCert(bean.getMaritalCert());
        } else if (type == PicEnum.creditCert.getId()) {
            picBean.setCreditCert(bean.getCreditCert());
        } else if (type == PicEnum.supplement.getId()) {
            picBean.setSupplement(bean.getSupplement());
        } else if (type == PicEnum.newCreditCert.getId()) {
            picBean.setCreditCert(bean.getCreditCert());
        } else if (type == PicEnum.newBusinessCert.getId()) {
            picBean.setBusinessCert(bean.getBusinessCert());
        } else if (type == PicEnum.newWarranty.getId()) {
            picBean.setCarWarranty(bean.getCarWarranty());
        }
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unRegister(this);
        super.onDestroy();
    }

    @Override
    public void setUI(ImgInfoBean bean) {
        this.imgInfoBean = bean;
        showUI();
    }

    @Override
    public void setData(CustomerBean customerBean) {
        this.customerBean=customerBean;
        if(orderType==OrderTypeEnum.personal.getKey()) {
            if ("true".equals(customerBean.getFund()) || "true".equals(customerBean.getHouse())) {
                if ("true".equals(customerBean.getFund()) && "false".equals(customerBean.getHouse())) {
                    house_text.setVisibility(View.VISIBLE);//选填文字提示
                }
                roomCert.setVisibility(View.VISIBLE);//公积金房产
            } else {
                roomCert.setVisibility(View.GONE);//公积金房产
            }
            // businessCert.setVisibility(View.VISIBLE);
            carWarranty.setVisibility("false".equals(customerBean.getInsurance()) ? View.GONE : View.VISIBLE);//保单
        }


    }


}
